from api.api import ElasticSearchQueryResource
from flask_restful import reqparse, Resource
from flask import Response
import base64


class ImageClassifyResource(ElasticSearchQueryResource):
    pass


class ImageSearchResource(ElasticSearchQueryResource):
    pass


class ImageSimilarityResource(ElasticSearchQueryResource):
    pass


class ImageInfoResource(ElasticSearchQueryResource):
    pass


class ImageGetResource(Resource):
    get_querier = None
    post_querier = None

    @classmethod
    def set_resource(cls, get_querier=None, post_querier=None):
        cls.get_querier = get_querier
        cls.post_querier = post_querier

    def get(self, img_id):
        print(img_id)
        b64_str = self.__class__.get_querier.search({'img_id': img_id})
        img = base64.b64decode(b64_str)
        resp = Response(img, mimetype='image/jpeg')
        return resp


img_search_parser = reqparse.RequestParser()
img_search_parser.add_argument('youka_id', type=str, location='json')
img_search_parser.add_argument('image_base64', type=str, location='json')
img_search_parser.add_argument('image_base64_test', type=str, location='json')
img_search_parser.add_argument('term', type=str, location='json')
img_search_parser.add_argument('from', type=int, location='json')
img_search_parser.add_argument('size', type=int, location='json')
img_search_parser.add_argument('filters', type=dict, location='json')

img_info_parser = reqparse.RequestParser()
img_info_parser.add_argument('youka_id', type=str, location='json')
img_info_parser.add_argument('article_id', type=str, location='json')
img_info_parser.add_argument('source', type=str, location='json')
